﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls.Evaluation
{
    public partial class CascadePKPIs : System.Web.UI.UserControl
    {
        public bool PublicButtonCascadeEnabled
        {
            get
            {
                return RadButtonCascade.Enabled;
            }
            set
            {
                RadButtonCascade.Enabled = value;
            }
        }
        public Panel PublicPanelPKPIs
        {
            get
            {
                return PanelPKPIs;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadKPIs();

                Session.Remove("FillCount");
            }
            else
            {
                PersistPKPIs();
            }
        }

        private void PersistPKPIs()
        {
            if (Session["FillCount"] != null)
            {
                int count = Int32.Parse(Session["FillCount"].ToString());

                for (int i = 0; i < count; ++i)
                {
                    FillPKPIDetail pd = (FillPKPIDetail)LoadControl("~/UserControls/Evaluation/FillPKPIDetail.ascx");
                    pd.ID = "Fill" + i;
                    PanelPKPIs.Controls.Add(pd);
                }
            }
        }

        protected void RadButtonCascade_Click(object sender, EventArgs e)
        {
            List<Personal_KPI> tempPKPIs = new List<Personal_KPI>();

            IList<RadTreeNode> allNodes = RadTreeView1.GetAllNodes();

            int count = 0;
            foreach (RadTreeNode n in allNodes)
            {
                if (n.Level == 3 && n.Selected == true)
                {
                    Personal_KPI pKPI = new Personal_KPI();
                    pKPI.Name = "(Personal) " + n.Text;
                    //pKPI.Description = dkpi.KPI.Description;
                    pKPI.KPI_FK = new int();
                    pKPI.KPI_FK = Int32.Parse(n.Value);

                    KPI k = new KPI();
                    k.KPI_ID = Int32.Parse(n.Value);
                    k.Name = n.Text;
                    pKPI.KPI = k;
                    //TODO //pKPI.User_FK = u.ID;

                    tempPKPIs.Add(pKPI);

                    FillPKPIDetail pd = (FillPKPIDetail)LoadControl("~/UserControls/Evaluation/FillPKPIDetail.ascx");
                    pd.ID = "Fill" + count++;
                    pd.PKPI = pKPI;
                    pd.DataBind();

                    PanelPKPIs.Controls.Add(pd);
                }

                if (Session["FillCount"] == null)
                {
                    Session.Add("FillCount", count);
                }
                else
                {
                    Session["FillCount"] = count;
                }
            }

            //if (Session["tempPKPIs"] == null)
            //{
            //    Session.Add("tempPKPIs", tempPKPIs);
            //}
            //else
            //{
            //    Session["tempPKPIs"] = tempPKPIs;
            //}

            //RadGridPersonalKPIs.DataSource = tempPKPIs;
            //RadGridPersonalKPIs.DataBind();
        }

        private void LoadKPIs()
        {
            RadTreeNode root = new RadTreeNode();
            
            root.Text = "Strategy";
            RadTreeView1.Nodes.Add(root);

            BSCEntities context = new BSCEntities();

            List<Strategic_Target> sts = (from st in context.Strategic_Target
                                          select st).ToList();

            foreach (Strategic_Target target in sts)
            {
                RadTreeNode stnode = new RadTreeNode();
                stnode.ImageUrl = "~/Images/KPI Drilling/targetIcon.jpg";

                stnode.Text = target.Name;
                stnode.Attributes["Description"] = target.Description;
                stnode.Attributes["NodeType"] = target.GetType().Name;

                root.Nodes.Add(stnode);

                List<Business_Solution> bss = (from bs in context.Business_Solution
                                               where bs.Strategic_Target_FK == target.Strategic_Target_ID
                                               select bs).ToList();

                foreach (Business_Solution solution in bss)
                {
                    RadTreeNode bsnode = new RadTreeNode();
                    bsnode.ImageUrl = "~/Images/KPI Drilling/questionmark.jpg";
                    bsnode.Text = solution.Solution;
                    bsnode.Attributes["Description"] = "";// solution.
                    bsnode.Attributes["NodeType"] = solution.GetType().Name;

                    stnode.Nodes.Add(bsnode);


                    List<KPI> kpis = (from kpi in context.KPIs
                                      where kpi.Solution_FK == solution.ID
                                      select kpi).ToList();

                    foreach (KPI k in kpis)
                    {
                        RadTreeNode node = new RadTreeNode();
                        node.ImageUrl = "~/Images/KPI Drilling/graph Icon.jpg";
                        node.Text = k.Name;
                        node.Value = k.KPI_ID.ToString();
                        //node.Attributes["aPosition"] = "position";
                        node.Attributes["Description"] = k.Description;
                        node.Attributes["NodeType"] = k.GetType().Name;


                        bsnode.Nodes.Add(node);

                        List<Personal_KPI> pkpis = (from pkpi in context.Personal_KPI.Include("User")
                                                    where pkpi.KPI_FK == k.KPI_ID
                                                    select pkpi).ToList();

                        foreach (Personal_KPI pk in pkpis)
                        {
                            RadTreeNode pnode = new RadTreeNode();
                            pnode.ImageUrl = "~/Images/KPI Drilling/icon-person.png";
                            pnode.Text = pk.Name;
                            //pnode.Attributes["aPosition"] = "bposition";
                            pnode.Attributes["Description"] = pk.Description;
                            pnode.Attributes["NodeType"] = pk.GetType().Name;
                            pnode.Attributes["UserID"] = pk.User_FK.ToString();
                            pnode.Attributes["Fullname"] = pk.User.Fullname;
                            node.Nodes.Add(pnode);
                        }
                    }
                }
            }
        }
    }
}